﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CopyUnit
{
    public class Program
    {
        static void Main(string[] args)
        {
            ShallowCopyTest();
            DeepCopyTest();
        }
        static void CopyTest()
        {
            CopyTest copytest = new CopyTest();
            Student s1 = new Student("Vivi", 28);
            copytest.obj = s1;
            Console.WriteLine("修改前:");
            Console.WriteLine("s1=[" + (Student)copytest.obj + "]");
            s1.Name = "tianyue";
            Console.WriteLine("修改后:");
            Console.WriteLine("s1=[" + (Student)copytest.obj + "]");
            Console.Read();
        }
        static void ShallowCopyTest()
        {

            Console.WriteLine("浅拷贝");
            Student s1 = new Student("Vivi", 28);
            Console.WriteLine("s1=[" + s1 + "]");
            Student s2 = (Student)s1.ShallowCopy();
            Console.WriteLine("s2=[" + s2 + "]"); //此处s2和s1内容相同 
            Console.WriteLine("-----------------------------");
            //修改s2的内容 
            s2.Name = "tianyue";
            s2.Age = 25;
            s2.Room.RoomId = 2;
            s2.Room.RoomName = "Room2";
            Console.WriteLine("s1=[" + s1 + "]");
            Console.WriteLine("s2=[" + s2 + "]");
            //再次打印两个对象以比较 
            Console.ReadLine();
        }
        static void DeepCopyTest()
        {
            Console.WriteLine("深拷贝");
            Student s1 = new Student("Vivi", 28);
            Console.WriteLine("s1=[" + s1 + "]");
            Student s2 = (Student)s1.DeepCopy();
            Console.WriteLine("s2=[" + s2 + "]"); //此处s2和s1内容相同 
            Console.WriteLine("-----------------------------");
            //修改s2的内容 
            s2.Name = "tianyue";
            s2.Age = 25;
            s2.Room.RoomId = 2;
            s2.Room.RoomName = "Room2";
            Console.WriteLine("s1=[" + s1 + "]");
            Console.WriteLine("s2=[" + s2 + "]");
            //再次打印两个对象以比较 
            Console.ReadLine();
        }
    }
}
